package com.itxiaowu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itxiaowu.dao.MemberDao;
import com.itxiaowu.dao.OrderDao;
import com.itxiaowu.service.ReportService;
import com.itxiaowu.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;


    /**
     * 查询运营数据
     * reportData:{
     *      *                     reportDate:null,
     *      *                     todayNewMember :0,
     *      *                     totalMember :0,
     *      *                     thisWeekNewMember :0,
     *      *                     thisMonthNewMember :0,
     *      *                     todayOrderNumber :0,
     *      *                     todayVisitsNumber :0,
     *      *                     thisWeekOrderNumber :0,
     *      *                     thisWeekVisitsNumber :0,
     *      *                     thisMonthOrderNumber :0,
     *      *                     thisMonthVisitsNumber :0,
     *      *                     hotSetmeal :[
     *      *                         {name:'阳光爸妈升级肿瘤12项筛查（男女单人）体检套餐',setmeal_count:200,proportion:0.222},
     *      *                         {name:'阳光爸妈升级肿瘤12项筛查体检套餐',setmeal_count:200,proportion:0.222}
     *      *                     ]
     *      *                 }
     *
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {
        // 准备数据
        Map<String,Object> map = new HashMap<>();
        //     本周一开始时间
        String firstDay = DateUtils.parseDate2String(DateUtils.getFirstDayOfWeek(new Date()));
        //     本月开始时间
        String thisMonthFirstDay = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //     本月最后一天
        String lastDateByMonth = DateUtils.parseDate2String(new Date(),"yyyy-MM") + "-31";

        // 1.1 报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        map.put("reportDate",today);

        // 今日新增会员
        Integer newMember = memberDao.findNewMemberByDate(today);
        map.put("todayNewMember",newMember);

        // 总会员数
        Integer totalMember = memberDao.findAllMember();
        map.put("totalMember",totalMember);

        // 本周新增会员
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(firstDay);
        map.put("thisWeekNewMember",thisWeekNewMember);

        // 本月新增会员
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(thisMonthFirstDay);
        map.put("thisMonthNewMember",thisMonthNewMember);

        // 今日新增预约
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        map.put("todayOrderNumber",todayOrderNumber);

        // 今日到访人数
        Integer todayVisitsNumber = orderDao.findVisitCountByDate(today);
        map.put("todayVisitsNumber",todayVisitsNumber);

        // 本周预约人数   -- > 预约可以往后推时间
        Map<String,String> hash = new HashMap<>();
        hash.put("startDate",firstDay);
        hash.put("endDate",today);
        Integer thisWeekOrderNumber = orderDao.findOrderCountBetweenDates(hash);
        System.out.println(" 本周预约人数是：" + thisWeekOrderNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);

        // 本周到访人数
        Integer thisWeekVisitsNumber = orderDao.findVisitCountByDate(firstDay);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);

        // 本月预约人数
        hash.put("startDate",thisMonthFirstDay);
        hash.put("endDate",lastDateByMonth);
        Integer thisMonthOrderNumber = orderDao.findOrderCountBetweenDates(hash);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);

        // 本月到访人数
        Integer thisMonthVisitsNumber = orderDao.findVisitCountByDate(thisMonthFirstDay);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

        // 1.2 hotsetmeal  数组形式的
        // 热门套餐前两名
        List<Map<String,Object>> hotSetmeal = orderDao.findHostSetmeal();
        map.put("hotSetmeal",hotSetmeal);

        return map;
    }

    public static void main(String[] args) throws Exception {
        // 查看本周一时间
//        String s = DateUtils.parseDate2String(DateUtils.getFirstDayOfWeek(new Date()));
//        System.out.println(s);
//        String thisMonthFirstDay = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
//        System.out.println(thisMonthFirstDay);

        String s = DateUtils.parseDate2String(new Date(), "yyyy-MM") + "-31";
        System.out.println(s);

    }
}
